<?php
namespace app\admin\model;
use app\base\model\BaseModel;
/**
 * 文件操作
 */
class FileModel extends BaseModel {
    //完成
    protected $_auto = array (
        array('time','time',3,'function'),
     );

    /**
     * 上传数据
     * @return array 文件信息
     */
    public function uploadData()
    {
        $upload = target('base/Upload');
        $config = array();
        $config['DIR_NAME'] = date('Y-m-d');
        $data = $upload->upload($config);
        if(!$data){
            $this->error = $upload->getError();
            return false;
        }
        $this->add($data);
        return $data;
    }

    public function deleteData($str){
        $fileurl = explode("/",dirname(__FILE__));
        $fileurl = "/".$fileurl[1]."/".$fileurl[2]."/".$fileurl[3].$str;
        $where['url']  = $str;
        $result = "false";
        if (file_exists ( $fileurl )) {
              unlink ( $fileurl );
         }
        $result = $this->where($where)->delete();
        if($result != "false") return $str;
    }

    public function loadList($where = array(), $limit = 0,$fields="*"){
        $data = $this->where($where)
                ->field($fields)
                ->limit($limit)
                ->select();
        return $data;
    }
    
    public function PublicQuery($table_name,$where = array(),$fields="*"){
        $imageUrl = array();
        $data = $this->table($table_name)
                ->where($where)
                ->field($fields)
                ->select();
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                $tmp = getimages($v);
                if(is_array($tmp)){
                    if(!is_null($tmp)){
                        foreach ($tmp as $ik => $iv) {
                            $imageUrl[] = $iv;
                        }
                    }
                }else{
                    $imageUrl[] = $tmp;
                }
            }
        }
        return $imageUrl;
    }
}
